home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / SCSI Samples 1.0 / SCSI DriveID Sample 06⁄07 ƒ / Src / ShowSCSIDeviceIdent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-16  |  4.3 KB  |  175 lines  |  [TEXT/KAHL]

  1. /*                                ShowSCSIDeviceIdent.c                            */
  2. /*
  3.  * ShowSCSIDeviceIdent.c
  4.  * Copyright © 1992-94 Apple Computer Inc. All Rights Reserved.
  5.  *
  6.  * This subroutine prints the bus ID or device ident associated with the
  7.  * specified driver.
  8.  */
  9. #include <stdio.h>
  10. #include <Files.h>
  11. #include <Devices.h>
  12. #include <Memory.h>
  13. #ifndef FALSE
  14. #define FALSE        0
  15. #define TRUE        1
  16. #endif
  17. /*
  18.  * Include the O.S. files in a specific order to make sure that we have
  19.  * a definition for the _SCSIAtomic trap.
  20.  */
  21. #include <Traps.h>
  22. #ifndef _SCSIAtomic
  23. #define _SCSIAtomic    0xA089
  24. #endif
  25. /*
  26.  * Note that this uses a later version of <Scsi.h> than is available in
  27.  * the published headers.
  28.  */
  29. #include "Scsi.h"
  30.  
  31. void                    ShowSCSIDeviceIdent(
  32.         short                driverRefNum
  33.     );
  34. Boolean                    AsyncSCSIPresent(void);
  35.  
  36. static void
  37. ClearMemory(
  38.         Ptr                ptr,
  39.         Size            size
  40.     )
  41. {
  42.         while (size > 0) {
  43.             *ptr++ = 0;
  44.             --size;
  45.         }
  46. }
  47.  
  48. /*
  49.  * The following macros are valid for the old SCSI Manager and for SCSI Manager
  50.  * 4.3 in the simple case where the device is the first registered on a bus
  51.  * and is registered under logical unit zero.
  52.  */
  53. #define DriverRefNumToSCSI(x)    ((signed short) (~(x) - 32))
  54. /*
  55.  * This macro converts a SCSI bus ID to a DriverRefNum
  56.  */
  57. #define SCSIToDriverRefNum(x)    ((signed short) (~((x) + 32)))
  58. /*
  59.  * These constants define the range of Driver RefNum's that are associated with
  60.  * hard-wired SCSI bus id's. Note that the driver reference numbers are negative.
  61.  * If a driver is present in this range, the device can be accessed through both
  62.  * the old and New SCSI Manager.
  63.  */
  64. #define kMinSCSIDrive    (SCSIToDriverRefNum(6))
  65. #define kMaxSCSIDrive    (SCSIToDriverRefNum(0))
  66.  
  67. Boolean                        IsHardWiredSCSIDevice(
  68.         register short            driverRefNum
  69.     );
  70. Boolean                        DriverRefNumToDeviceIdent(
  71.         register short            driverRefNum,
  72.         DeviceIdent                *deviceIdentPtr
  73.     );
  74.  
  75. void
  76. ShowSCSIDeviceIdent(
  77.         short                driverRefNum
  78.     )
  79. {
  80.         DeviceIdent            deviceIdent;
  81.         
  82.         if (DriverRefNumToDeviceIdent(driverRefNum, &deviceIdent)) {
  83.             /*
  84.              * This device is registered with SCSI Manager 4.3
  85.              */
  86.             printf(
  87.                 " -- [%d.%d.%d] (registered)",
  88.                 (int) deviceIdent.bus,
  89.                 (int) deviceIdent.targetID,
  90.                 (int) deviceIdent.LUN
  91.             );
  92.         }
  93.         else if (IsHardWiredSCSIDevice(driverRefNum)) {
  94.             /*
  95.              * This is a SCSI device whose driver did not register
  96.              * with SCSI Manager 4.3, possibly because the new
  97.              * SCSI Manager is not installed on this machine.
  98.              */
  99.             printf(" -- [%d] (hardwired)",
  100.                 (int) DriverRefNumToSCSI(driverRefNum));
  101.         }
  102.         else {
  103.             /*
  104.              * This is not a SCSI device.
  105.              */
  106.             printf(" -- Not SCSI");
  107.         }
  108. }
  109.  
  110. /*
  111.  * If SCSI Manager 4.3 is present and this driver has been registered, return
  112.  * TRUE and store the SCSI Device Ident in the deviceIdentPtr location. If SCSI
  113.  * Manager 4.3 is not present, or this is not a registered driver, return FALSE.
  114.  *
  115.  * Note that this returns FALSE even if SCSI Manager 4.3 is present if the
  116.  * device's driver did not register with the new SCSI Manager (i.e. if the
  117.  * device uses an "old-style" driver).
  118.  */
  119. Boolean
  120. DriverRefNumToDeviceIdent(
  121.         register short            driverRefNum,
  122.         DeviceIdent                *deviceIdentPtr
  123.     )
  124. {
  125.         SCSIDriverPB            pb;
  126.         OSErr                    status;
  127.         
  128.         if (AsyncSCSIPresent()) {
  129.             ClearMemory((Ptr) &pb, sizeof pb);
  130.             pb.scsiPBLength = sizeof (SCSIDriverPB);
  131.             pb.scsiCompletion = NULL;
  132.             pb.scsiFlags = 0;
  133.             pb.scsiFunctionCode = SCSILookupRefNumXref;
  134.             * ((long *) &pb.scsiDevice) = 0xFFFFFFFFL;
  135.             do {
  136.                 status = SCSIAction((SCSI_PB *) &pb);
  137.                 if (status != noErr)
  138.                     break;
  139.                 else if (pb.scsiDriver == driverRefNum
  140.                       && pb.scsiDevice.bus != 0xFF) {
  141.                     *deviceIdentPtr = pb.scsiDevice;
  142.                     return (TRUE);                        /* Success                */
  143.                 }
  144.                 else {
  145.                     pb.scsiDevice = pb.scsiNextDevice;
  146.                 }
  147.             }
  148.             while (pb.scsiDevice.bus != 0xFF);
  149.         }
  150.         /*
  151.          * Exit here if either the New SCSI Manager is no present or
  152.          * the device is not a SCSI device.
  153.          */
  154.          return (FALSE);
  155. }
  156.  
  157.                 
  158.  
  159. /*
  160.  * TRUE if this driver is a SCSI device. Note: this is valid only for the Old SCSI
  161.  * Manager and for the New SCSI Manager in a simple bus configuration where all
  162.  * devices have unique target ID's and all SCSI devices are at logical unit zero.
  163.  */
  164. Boolean
  165. IsHardWiredSCSIDevice(
  166.         register short            driverRefNum
  167.     )
  168. {
  169.         return (
  170.             DriverRefNumToSCSI(driverRefNum) >= 0
  171.             && DriverRefNumToSCSI(driverRefNum) <= 6
  172.         );
  173. }
  174.  
  175.